<template>
  <div>
    <el-dialog :title="form.id ? '查看部门' : '新增部门'" :visible="dialogShow">
      <el-form :model="form" label-width="100px" :rules="rules" ref="myform">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" placeholder="1-50个字符"></el-input>
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" placeholder="1-50个字符"></el-input>
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <!-- <el-input v-model="form.manager" placeholder="请选择"></el-input> -->
          <el-select v-model="form.manager" placeholder="请选择">
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.username"
              :value="item.username"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="form.introduce"
            placeholder="1-300个字符"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitInfo">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDepartments,
  getSimple,
  addDepartments,
  editDepartments,
  editInfo
} from '@/api/departments'
export default {
  props: ['dialogShow', 'id'],
  data () {
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      list: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '字数1-50', trigger: 'blur' },
          {
            validator: async (rule, value, callback) => {
              const res = await getDepartments()
              console.log(res)
              console.log(this.id)
              if (!this.form.id) {
                //新增
                res.data.depts.some(
                  item => item.pid == this.id && item.name == value
                )
                  ? callback(new Error('同级部门禁止同名'))
                  : callback()
              } else {
                //编辑
                res.data.depts.some(
                  item =>
                    item.pid == this.id &&
                    item.name == value &&
                    item.id !== this.form.id
                )
                  ? callback(new Error('同级部门禁止同名'))
                  : callback()
              }
              //   const result =  res.data.depts.filter(item=>{
              //       return item.pid == this.id
              //   })
              //   const bool = result.some(item=>{
              //       return item.name == value
              //   })
              //   console.log(bool);
              //   bool ? callback(new Error('同级部门禁止同名')) : callback()
              // res.data.depts.some(item=>{
              //     return item.pid == this.id && item.name == value
              // }) ?callback(new Error('同级部门禁止同名')) : callback()
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '字数1-50', trigger: 'blur' },
          {
            validator: async (rule, value, callback) => {
              const res = await getDepartments()
              if (!this.form.id) {
                //新增
                const bool = res.data.depts.some(item => {
                  return item.code == value
                })
                bool ? callback(new Error('编码不能相同')) : callback()
              } else {
                //编辑
                const bool = res.data.depts.some(item => {
                  return item.code == value && item.id !== this.form.id
                })
                bool ? callback(new Error('编码不能相同')) : callback()
              }
            },
            trigger: 'blur'
          }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '字数1-150', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //关闭弹框
    closeDialog () {
      this.$emit('update:dialogShow', false)
      //清空表单
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      //重置校验
      this.$refs.myform.resetFields()
    },
    //提交数据
    submitInfo () {
      this.$refs.myform.validate(async res => {
        //全局表单验证成功
        if (res) {
          //新增部门提交
          if (!this.form.id) {
             await addDepartments({ ...this.form, pid: this.id })
            
          } else {
            //编辑部门提交
            await editInfo(this.form)
          }
          
            this.$message.success( this.form.id ? '编辑成功' : '新增成功')
            // this.$emit('closeDialog')
            this.$emit('update:dialogShow', false)
            //更新数据
            this.$emit('resData')
            //清空表单
            this.form = {
              name: '',
              code: '',
              manager: '',
              introduce: ''
            }
        } else {
        }
      })
    },
    //编辑
    async editDepartments (id) {
      const res = await editDepartments(id)
      console.log(res)
      this.form = res.data
    }
  },
  async created () {
    //获得简单人员数据
    let res = await getSimple()
    console.log(res)
    this.list = res.data
  }
}
</script>

<style></style>
